using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CPGVertragsverwaltung.ErrorHandling;
using System.Diagnostics;

namespace CPGVertragsverwaltung.Viewer
{
    public partial class MiniObjectItem : UserControl
    {
        private Color mNormalBackColor, mMouseoverBackColor, mSelectedBackColor;
        private Boolean mIsHovering = false;
        private Boolean mIsSelected = false;

        #region events
        public delegate void SeletedDelegate(MiniObjectItem pItem);
        public event SeletedDelegate Selected;
        #endregion
        #region Properties
        //Properties
        String mKey;
        bool mIsInactive;
        int mAttachmentsCount;

        public String Key
        {
            set { mKey = value; }
            get { return mKey; }
        }

        public String Text1
        {
            set
            {
                lblText1.Text = value;
            }
            get { return lblText1.Text; }
        }

        public String Text2
        {
            set { lblText2.Text = value; }
            get { return lblText2.Text; }
        }

        public String Text3
        {
            set { lblText3.Text = value; }
            get { return lblText3.Text; }
        }


        public bool IsInactive
        {
            set
            {
                mIsInactive = value;
                if (mIsInactive)
                {
                    this.BackColor = Color.GreenYellow;
                }
                else
                {
                    this.BackColor = Color.White;
                }
            }
            get { return mIsInactive; }
        }

        public int AttachmentsCount
        {
            set
            {
                mAttachmentsCount = value;
                if (mAttachmentsCount > 0)
                {
                    picAttachment.Image = CPGVertragsverwaltung.Properties.Resources.attachment;
                    picAttachment.Visible = true;
                    lblCount.Text = mAttachmentsCount.ToString();
                }
                else
                {
                    picAttachment.Image = null;
                    picAttachment.Visible = false;
                    lblCount.Text = "";
                }
            }
            get { return mAttachmentsCount; }
        }

        public Boolean IsSelected
        {
            get { return mIsSelected; }
            set
            {
                mIsSelected = value;
                if (mIsSelected)
                {
                    this.BackColor = mSelectedBackColor;
                }
                else
                {
                    this.BackColor = mNormalBackColor;
                }
            }
        }
        #endregion

        public MiniObjectItem()
        {
            InitializeComponent();

            //Set BackColor
            mNormalBackColor = this.BackColor;
            mMouseoverBackColor = Color.FromArgb(255, 222, 237, 255);
            mSelectedBackColor = Color.FromArgb(255, 210, 230, 255);


            //All MouseLeave Events will be checked by UserControl
            foreach (Control myControl in this.Controls)
            {
                myControl.MouseLeave += new EventHandler(MiniObjectItem_MouseLeave);
                myControl.Click += new EventHandler(MiniObjectItem_Click);
            }
        }

        /// <summary>
        /// repositioning object
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MiniObjectItem_Resize(object sender, EventArgs e)
        {
            lblText3.Left = this.Width - (3 + 80);
            lblText3.Top = 3;
            lblText3.Width = 80;

            picAttachment.Left = this.Width - (3 + 15);
            picAttachment.Top = this.Height - (3 + 15);
            picAttachment.Width = 15;

            lblCount.Left = picAttachment.Left - 60;
            lblCount.Top = this.Height - (3 + lblCount.Height);
            lblCount.Width = 60;

            lblText1.Left = 3;
            lblText1.Top = 3;
            lblText1.Width = lblText3.Left - 3;

            lblText2.Left = 3;
            lblText2.Top = this.Height - (3 + lblText2.Height);
            lblText2.Width = lblCount.Left - 3;
        }

        /// <summary>
        /// Capture Hovering Effect
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MiniObjectItem_MouseEnter(object sender, EventArgs e)
        {
            if (!mIsHovering)
            {
                mIsHovering = true;
                if (!mIsSelected)
                {
                    this.BackColor = mMouseoverBackColor;
                }
            }
        }

        private void MiniObjectItem_MouseDown(object sender, MouseEventArgs e)
        {

        }

        /// <summary>
        /// Disable Hovering Effect
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MiniObjectItem_MouseLeave(object sender, EventArgs e)
        {
            if (!this.ClientRectangle.Contains(PointToClient(Control.MousePosition)))
            {
                mIsHovering = false;
                if (!mIsSelected)
                {
                    this.BackColor = mNormalBackColor;
                }
            }
        }

        /// <summary>
        /// Change background and fire event when object is selected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MiniObjectItem_Click(object sender, EventArgs e)
        {
            select();

            if (this.Selected != null)
            {
                this.Selected(this);
            }
        }

        /// <summary>
        /// Select Item, set Backgroundcolor and mIsSelected
        /// </summary>
        public void select()
        {
            this.BackColor = mSelectedBackColor;
            this.mIsSelected = true;
        }

        /// <summary>
        /// Deselect Item, set BackColor and mIsSelected
        /// </summary>
        public void deselect()
        {
            this.BackColor = mNormalBackColor;
            this.mIsSelected = false;
        }
    }
}
